﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.Script.Services;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace ProcolServices {
    /// <summary>
    /// Summary description for IdeaUpdatesService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [ScriptService]
    public class IdeaUpdatesService : System.Web.Services.WebService {
        private readonly ProcolDataContext db = new ProcolDataContext();

        [WebMethod]
        public int AddIdeaUpdate(String entryId, int ideaId,int origin, int target, DateTime timeStamp) {
            var values = new object[] { entryId, ideaId, origin, target, timeStamp };
            foreach (var value in values) {
                String str = value.ToString();
                int tryParse;

                if (Int32.TryParse(str, out tryParse)) {
                    if (tryParse < 0)
                        return -2;
                }
                else if (String.IsNullOrEmpty(str))
                    return -2;
            }

            try {

                var newIdeaUpdt = new IdeaUpdate {
                    EntryId = entryId,
                    IdeaId = ideaId,
                    Origin = origin,
                    Target = target,
                    Timestamp = DateTime.Now,
                };

                db.IdeaUpdates.InsertOnSubmit(newIdeaUpdt);
                db.SubmitChanges();

                return newIdeaUpdt.UpdateId;
            }catch (InvalidOperationException) {
                return -1;
            }
        }
    }
}
